const { createClient } = require('redis');

const client = createClient();

client.on('error', (err) => console.log('Redis Client Error', err));

const setRedisValue = async (key, value) => {
    if (!client.isOpen) {
        await client.connect();
    }
    await client.set(key, value);
}

const setRedisMap = async (key, field, value) => {
    if (!client.isOpen) {
        await client.connect();
    }
    await client.hSet(key, field, value);
}

const getRedisValue = async (key) => {
    if (!client.isOpen) {
        await client.connect();
    }
    return await client.get(key);
}


const getRedisMap = async (key, field) => {
    if (!client.isOpen) {
        await client.connect();
    }
    return await client.hGet(key, field);
}


module.exports = {
    setRedisValue,
    getRedisValue,
    setRedisMap,
    getRedisMap
}